<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\GoodsType;
use App\Http\Controllers\Controller;
class GoodsTypeController extends Controller
{
    protected $model;

    public function __construct(GoodsType $goodsType)
    {
        $this->model = $goodsType;
    }

    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     * 分类列表
     */

    public function index(Request $request)
    {
        
        $user = auth('admin')->user();
        $userid = $user->id;
        if($user->type == 4){
            $userid = $user->parent_id;
        }
        $result = $this->model
            ->filter($request->all())
            ->where('state', 1)
            ->where('merchant_id', $userid)
            ->orderBy('created_at', 'desc')
            ->paginate(config('api.page.limit'));
        return view('admins.goodstype.index')->with('result', $result);
    }
	/*
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $merchant = auth('admin')->user();
        $merchantId = $merchant->id;
        if($merchant->type == 4){
            $merchantId = $merchant->parent_id;
        }
        $goodsName = $request->goodsName;
        $time = date('Y-m-d H:i:s', time());
        $data = array(
            'goods_name' => $goodsName,
            'merchant_id' => $merchantId,
            'state' => 1,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result = 0;  
        if($data){
            $result = $this->model->insertGetId($data);
            $msg = '添加失败，请重试！';
            if ($result) {
                $msg = '添加成功！';
            }
        }
        return response()->json(['flag' => $result, 'msg' => $msg]);
    }
    public function create(Request $request)
    {
        $categs = GoodsType::getcates();
        return view('admins.goodstype.add')->with("categs", $categs);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $merchant = auth('admin')->user();
        $merchantId = $merchant->id;
        if($merchant->type == 4){
            $merchantId = $merchant->parent_id;
        }
        $result = [];
        if ($id) {
            $result = $this->model
                ->where('state', 1)
                ->where('merchant_id', $merchantId)->find($id);
               
        }
        return view('admins.goodstype.edit')->with("result", $result);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->id;
        $merchant = auth('admin')->user();
        $merchantId = $merchant->id;
        if($merchant->type == 4){
            $merchantId = $merchant->parent_id;
        }
        $goodsName = $request->goodsName;
        $result = $this->model
            ->where('id',$id)
            ->where('merchant_id', $merchantId)   
            ->update(['goods_name'=>$goodsName]);

        $msg = '修改失败，请重试！';
        if ($result) {
            $msg = '修改成功！';
        }
        return response()->json(['flag' => $result, 'msg' => $msg]);
	}

    public function destroy($id)
    {
        $merchantId = auth('admin')->user()->id;
        if ($id) {
            $result = $this->model
                ->where('id', $id)
                ->where('merchant_id', $merchantId)
                ->update(['state' => 0]);
        }
      return redirect()->route('admin.goodstype.index');
    }
}
